/*
 *  Copyright (C) 2005-2007 Renier Roth
 *  www.renier.de
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package de.renier.jkeepass.actions;

import java.awt.event.ActionEvent;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import de.renier.jkeepass.JKeePass;
import de.renier.jkeepass.Messages;
import de.renier.jkeepass.dialog.TanInputDialog;
import de.renier.jkeepass.elements.BasicElement;
import de.renier.jkeepass.elements.ContainerElement;

/**
 * CreateTanElementAction
 *
 * @author <a href="mailto:software@renier.de">Renier Roth</a>
 */
public class CreateTanElementAction extends AbstractAction {
  
  private static final long serialVersionUID = -2584160468923427168L;

  public CreateTanElementAction() {
    super(Messages.getString("CreateTanElementAction.0"),new ImageIcon(CreateTanElementAction.class.getResource("/org/javalobby/icons/20x20/NewRow.gif"))); //$NON-NLS-1$ //$NON-NLS-2$
    this.setEnabled(false);
  }
  
  /* (non-Javadoc)
   * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
   */
  public void actionPerformed(ActionEvent e) {
    // input dialog for container
    TanInputDialog tanDialog = new TanInputDialog(JKeePass.application);
    tanDialog.show();
    
    // do we save it
    if (TanInputDialog.RESULT_OK == tanDialog.getResult()) {
      // get the element
      String[] tans = tanDialog.getTanArray();
      if (tans != null && tans.length > 0) {
        // get container
        TreePath treePath = JKeePass.application.getElementTreePanel().getLeadSelectionPath();
        if (treePath != null) {
          DefaultMutableTreeNode node = (DefaultMutableTreeNode)treePath.getLastPathComponent();
          if (node != null) {
            ContainerElement currentContainer = (ContainerElement)node.getUserObject();
            Date now = new Date();
            String icon = tanDialog.getSymbol();
            int offset = tanDialog.getOffset();
            int numLen = (tanDialog.getFieldSize() <= 0) ? String.valueOf(offset + tans.length).length() : tanDialog.getFieldSize();
            // create one entry for each tan
            for (int i = 0; i < tans.length; i++) {
              // create Basic ELement for a TAN
              BasicElement basicElement = new BasicElement(Messages.getString("CreateTanElementAction.2"),icon); //$NON-NLS-1$
              basicElement.setUser(prependZeroString(offset+i,numLen));
              basicElement.setPassword(tans[i]);
              basicElement.setComment(Messages.getString("CreateTanElementAction.3") + SimpleDateFormat.getDateTimeInstance().format(now)); //$NON-NLS-1$
              currentContainer.addBasicElement(basicElement);
            }
            // refresh
            JKeePass.application.getContainerElementPanel().update(currentContainer);
            JKeePass.application.setModified(true);
          }
        }
      }
    }
  }
  
  /**
   * prependZeroString
   *
   * @param num
   * @param len
   * @return
   */
  private String prependZeroString(int num, int len) {
    StringBuffer buffer = new StringBuffer();
    buffer.append(num);
    for (int i = buffer.length(); i < len; i++) {
      buffer.insert(0, "0"); //$NON-NLS-1$
    }
    return buffer.toString();
  }

}
